package com.cy.dda.web.front.diligencing;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.diligencing.DiligencingReportSaveAO;
import com.cy.dda.model.domain.diligencing.DiligencingAttachmentType;
import com.cy.dda.model.vo.diligencing.DiligencingReportAttachmentVO;
import com.cy.dda.model.vo.diligencing.DiligencingReportDetailVO;
import com.cy.dda.service.diligencing.DiligencingReportService;
import com.cy.dda.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 前台-尽调报告管理
 * @Author tanyilin
 * @Date 2019/6/3 15:37
 */
@RestController
@RequestMapping("/user/diligencing/report")
@Api(tags = "前台-尽调报告管理")
public class DiligencingReportController extends BaseController {

    @Autowired
    private DiligencingReportService diligencingReportService;

    @ApiOperation("尽调报告详情")
    @GetMapping("/get/detail")
    public Result<DiligencingReportDetailVO> getDiligencingReportDetail(@RequestParam(value = "diligencingId") Long diligencingId) {

        DiligencingReportDetailVO detailVO = diligencingReportService.getDiligencingReportDetail(diligencingId);
        return ok("获取尽调报告成功".concat(CommonUtils.isEmpty(detailVO) ? "，但无数据" : ""), detailVO);
    }

    @ApiOperation("保存尽调报告")
    @PostMapping("/do/save")
    public Result doSaveReport(@Validated @RequestBody DiligencingReportSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Result<Map<String, Object>> saveResult = diligencingReportService.saveDiligencingReport(saveAO, this.getUser().getUserId());

        return saveResult;
    }

    @ApiOperation("提交尽调报告")
    @PostMapping("/do/submit")
    public Result doSubmitReport(@Validated @RequestBody DiligencingReportSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        diligencingReportService.submitDiligencingReport(saveAO, this.getUser().getUserId());

        return ok();
    }

    @ApiOperation("保存新增影音资料类型")
    @PostMapping("/do/save/attachment/type")
    public Result doSaveAttachmentType(@RequestParam(value = "diligencingId") Long diligencingId, @RequestParam(value = "type") String type) {

        DiligencingAttachmentType diligencingAttachmentType = new DiligencingAttachmentType();
        diligencingAttachmentType.setDiligencingId(diligencingId);
        diligencingAttachmentType.setType(type);
        diligencingReportService.doSaveAttachmentType(diligencingAttachmentType);

        diligencingAttachmentType.setId(null);
        diligencingAttachmentType.setCreateTime(null);
        diligencingAttachmentType.setUpdateTime(null);

        return ok("保存照片类型成功", diligencingAttachmentType);
    }

    @ApiOperation("获取报告照片列表")
    @GetMapping("/get/attachment/list")
    public Result<List<DiligencingReportAttachmentVO>> getAttachmentList(@RequestParam(value = "diligencingReportId") Long diligencingReportId, @RequestParam(value = "type") String type) {

        List<DiligencingReportAttachmentVO> list = diligencingReportService.getDiligencingReportAttachment(diligencingReportId, type);

        return ok("获取照片列表成功", list);
    }

    @ApiOperation("获取新增的照片类型")
    @GetMapping("/get/attachment/type/list")
    public Result<List<DiligencingAttachmentType>> getDiligencingAttachmentType(@RequestParam(value = "diligencingId") Long diligencingId) {

        List<DiligencingAttachmentType> list = diligencingReportService.getDiligencingAttachmentType(diligencingId);

        return ok("获取新增的照片类型成功", list);
    }

}
